package net.ojbk.dao;

import java.sql.*;

import net.ojbk.database.DatabaseConnection;
import net.ojbk.po.ActivationData; // po层  数据实体类

/**
 * ActivationData Table
 * 
 * 增加 一条记录
 * 删除 一条记录
 * 查询 一条记录
 * 修改 一条记录
 * 
 * @author twobox
 *
 */
public class ActivationDataDao {
	static final String tableName = "activationData";
	DatabaseConnection dbconn; // 数据库连接控制对象
	Statement stmt; // sql语句 操作对象
	
	public ActivationDataDao() {
		dbconn = new DatabaseConnection();
		stmt = dbconn.getStatement();
	}
	public void finalize() {
		dbconn.close();
	}
	
	// activationData(email, code, password)
	
	public boolean addOneRecord(ActivationData ad) {
		boolean flag = false;
		try {
			String sql = "INSERT INTO "
					+ tableName
					+ " VALUE('"
					+ ad.getEmail()
					+ "', '"
					+ ad.getCode()
					+ "', '"
					+ ad.getPassword()
					+ "');";
			stmt.executeUpdate(sql);
			flag = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deleteOneRecord(ActivationData ad) {
		boolean flag = false;
		try {
			// DELETE FROM activationData WHERE email='1@ojbk.net';
			String sql = "DELETE FROM "
					+ tableName
					+ " WHERE email='"
					+ ad.getEmail()
					+ "';";
			stmt.executeUpdate(sql);
			flag = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean updateOneRecord(ActivationData ad) {
		boolean flag = false;
		try {
			// UPDATE activationData SET code='124412412jk4kj124k', password='654321' WHERE email='1@ojbk.net';
			String sql = "UPDATE "
					+ tableName
					+ " SET email='"
					+ ad.getEmail()
					+ "', code='"
					+ ad.getCode()
					+ "', password='"
					+ ad.getPassword()
					+ "' WHERE email='"
					+ ad.getEmail()
					+ "';";
			stmt.executeUpdate(sql);
			return true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean queryOneRecord(ActivationData ad) {
		boolean flag = false;
		ResultSet re;
		try {
			// SELECT * FROM activationData WHERE email='1@ojbk.net';
			String sql = "SELECT * FROM "
					+ tableName
					+ " WHERE code='"
					+ ad.getCode()
					+ "' or email='"
					+ ad.getEmail()
					+ "';";
			re = stmt.executeQuery(sql);
			if (re.next()) {
				ad.setEmail(re.getString("email"));
				ad.setCode(re.getString("code"));
				ad.setPassword(re.getString("password"));
				flag = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public static void main(String[] args) {
	}
	
}
